package com.fh.service.impl;/**
 * 描述:
 * Author:袁亚强
 * Date:2021/11/2112:00
 */

import com.alibaba.fastjson.JSON;
import com.fh.entity.po.Area;
import com.fh.entity.po.Type;
import com.fh.mappers.TypeMapper;
import com.fh.service.TypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

/**
 * created by macro on 2021/11/21,
 */
@Service
public class TypeServiceImpl implements TypeService {
    @Resource
    private TypeMapper typeMapper;
    @Autowired
    private RedisTemplate redisTemplate;

    private  StringBuffer sb=null;
    @Override
    public List<Map<String, Object>> queryAllTypeData() {
        List<Type> types = typeMapper.queryTypeData();
        return getTypeList(types, 0);
    }

    @Override
    public void deleteTypeById(Integer id) {
        typeMapper.deleteTypeById(id);
    }

    @Override
    public void addTypeData(Type type) {
        type.setCreateDate(new Date());
        type.setIsdel(0);
        typeMapper.addTypeData(type);
    }

    @Override
    public Type queryById(Integer id) {
        return typeMapper.queryById(id);
    }

    @Override
    public void updateType(Type type) {
        typeMapper.updateType(type);
    }

    @Override
    public List<Map> queryAllDataStr() {
        List<Map> rs=new ArrayList<>(); // 返回的数据

        //先得到所有的分类数据
        List<Type> types = typeMapper.queryTypeData();
        //遍历所有的数据
        for (Type t :types) {
            //判断t是否为父节点
            boolean parent = isParent(t.getId(), types);
            if(parent==false){
                Map m=new HashMap();
                m.put("id",t.getId());
                sb=new StringBuffer(t.getName());
                getCildrenName(t.getPid(),types);

                m.put("name",getRevertStr(sb));
                rs.add(m);
            }
        }
        return rs;
    }

    @Override
    public List<Type> query() {
        return typeMapper.query();
    }

    @Override
    public void reflushType() {
        //清除缓存
        redisTemplate.delete("typeData");
        // 添加缓存
        List<Type> type = this.query();
        redisTemplate.opsForValue().set("typeData", JSON.toJSONString(type));
    }

    private String getRevertStr(StringBuffer sb){
        StringBuffer str=new StringBuffer();
        // 789/456/123
        String[] split = sb.toString().split("/");
        List<String> strings = Arrays.asList(split);
        Collections.reverse(strings);
        for(String t :strings){
            str.append(t).append("/");
        }
        return str.substring(0,str.length()-1).toString();

    }

    /*
     * 判断当前id 是否有子节点 */
    private boolean isParent(Integer id,List<Type> datas){
        for (Type t :datas) {
            if(t.getPid().equals(id)){
                return true;
            }
        }
        return false;
    }

    private void getCildrenName(Integer pid,List<Type> datas){
        for (int i = 0; i < datas.size(); i++) {
            Type type=datas.get(i);
            if(type.getId().equals(pid)){
                sb.append("/").append(type.getName());
                if(!type.getPid().equals(0)){
                    getCildrenName(type.getPid(),datas);
                }
                break;
            }
        }
    }

    private List<Map<String, Object>> getTypeList(List<Type> types, Integer pid) {
        List<Map<String, Object>> list = new ArrayList<>();
        for (Type type : types) {
            Map<String, Object> map = null;
            if (pid.equals(type.getPid())) {
                map = new HashMap<>();
                map.put("id", type.getId());
                map.put("name", type.getName());
                map.put("img", type.getImg());
                map.put("children", getTypeList(types, type.getId()));
                List children = (List) map.get("children");
                if (children.size() == 0) {
                    map.remove("children");
                }
            }
            if (map != null) {
                list.add(map);
            }
        }
        return list;
    }


}
